package com.qire.common.receiver;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.wifi.WifiManager;

import com.qire.common.SummerApp.AppActiveRegistrar;
import com.qire.common.utils.NetworkUtils;
import com.qire.common.utils.NetworkUtils.NetworkType;
import com.qire.common.utils.ToastHelper;
import com.qire.common.widget.snackbar.Prompt;

/**
 * 监听网络状态变化广播接收器
 */
public class NetWorkChangReceiver extends BroadcastReceiver implements AppActiveRegistrar {

    private static volatile NetworkType lastNetworkType = NetworkType.NETWORK_NONE;

    private final ConnectivityAction connectivityAction;

    private boolean isRegistered = false;


    public NetWorkChangReceiver() {
        this.connectivityAction = new SimpleConnectivityAction();
    }

    public NetWorkChangReceiver(ConnectivityAction connectivityAction) {
        this.connectivityAction = connectivityAction;
    }

    public void registerReceiver(Context context) {

        if(isRegistered)
            return;
        isRegistered = true;
        IntentFilter filter = new IntentFilter();
        filter.addAction(WifiManager.WIFI_STATE_CHANGED_ACTION);
        filter.addAction(WifiManager.NETWORK_STATE_CHANGED_ACTION);
        filter.addAction(ConnectivityManager.CONNECTIVITY_ACTION);
        context.registerReceiver(this, filter);

        return;
    }

    public void unregisterReceiver(Context context) {
        if(isRegistered) {
            isRegistered = false;
            context.unregisterReceiver(this);
        }
    }

    @Override
    public void onReceive(Context context, Intent intent) {

        // 监听wifi的打开与关闭，与wifi的连接无关
//        if (WifiManager.WIFI_STATE_CHANGED_ACTION.equals(intent.getAction())) {
//            int wifiState = intent.getIntExtra(WifiManager.EXTRA_WIFI_STATE, 0);
//            Log.e("TAG", "wifiState:" + wifiState);
//            switch (wifiState) {
//                case WifiManager.WIFI_STATE_DISABLED:
//                    connectivityAction.sendEvent("当前WIFI被禁用了");
//                    break;
//                case WifiManager.WIFI_STATE_DISABLING:
//                    break;
//            }
//        }

        // 监听网络连接，包括wifi和移动数据的打开和关闭,以及连接上可用的连接都会接到监听
        if (ConnectivityManager.CONNECTIVITY_ACTION.equals(intent.getAction())) {
            //获取联网状态的NetworkInfo对象
            NetworkInfo info = intent.getParcelableExtra(ConnectivityManager.EXTRA_NETWORK_INFO);
            if (info != null) {
                //如果当前的网络连接成功并且网络连接可用
                if (NetworkInfo.State.CONNECTED == info.getState() && info.isAvailable()) {
                    NetworkType networkType = NetworkUtils.getNetworkType(context);
                    connectivityAction.connected(networkType);
                } else {
                    connectivityAction.disconnected(NetworkType.NETWORK_NONE);
                }
            }
        }

    }

    public interface ConnectivityAction {
        void connected(NetworkType networkType);
        void disconnected(NetworkType networkType);
        void sendEvent(String eventMSG);
    }

    public class SimpleConnectivityAction implements ConnectivityAction {

        @Override
        public void connected(NetworkType networkType) {

            // 避免每次切到前台都显示提醒
            if(networkType == lastNetworkType) {
                return;
            }

            lastNetworkType = networkType;

            if(networkType == NetworkType.NETWORK_WIFI) {
                ToastHelper.showSnackBar("正在使用Wifi网络，赶紧畅快阅读吧！", Prompt.SUCCESS);
            } else {
                ToastHelper.showSnackBar("正在使用2G/3G/4G网络，注意流量哟！", Prompt.WARNING);
            }
        }

        @Override
        public void disconnected(NetworkUtils.NetworkType networkType) {
            // 避免每次切到前台都显示提醒
            if(networkType == lastNetworkType) {
                return;
            }
            lastNetworkType = networkType;
            ToastHelper.showSnackBar("网络已断开", Prompt.WARNING);
        }

        @Override
        public void sendEvent(String eventMSG) { }

    }
}